import java.security.Key;

public class BinarySerachTree {
    public static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;

    public boolean search(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val > key) {
                cur = cur.left;
            } else if (cur.val < key) {
                cur = cur.right;
            } else {
                return true;
            }
        }
        return false;
    }

    /*public boolean insert(int key){
        TreeNode cur=root;
        TreeNode parent=null;
        if (root==null){
            root=new TreeNode(key);
            return true;
        }
        while (cur!=null){
            if (cur.val<key){
                parent=cur;
                cur=cur.right;
            }else if(cur.val>key){
                parent=cur;
                cur=cur.left;
            }else {
                return false;
            }
        }
        TreeNode node =new TreeNode(key);
        if (parent.val>key){
            parent.left=node;
        }else {
            parent.right=node;
        }
        return true;
    }
   */
    public boolean insert(int key) {
        if (root == null) {
            root = new TreeNode(key);
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else {
                return false;
            }
        }
        TreeNode node = new TreeNode(key);
        if (parent.val < key) {
            parent.right = node;
        } else {
            parent.left = node;
        }
        return true;
    }

    public void remove(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else {
                removeNode(cur, parent);
            }
        }
    }

    private void removeNode(TreeNode cur, TreeNode parent) {
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.left;
            } else {
                parent.right = cur.right;

            }
        }else {
            TreeNode target=cur.right;
            TreeNode targetParent=cur;
            while (target.left!=null){
                targetParent=target;
                target=target.left;
            }
            cur.val=target.val;
            if (targetParent.left==target){
                targetParent.left=target.right;
            }else {
                targetParent.right=target.right;
            }
        }
    }
}
